﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace HoSoLuuTru
{
    class ClsConnection
    {
        static MySqlConnection m_conn = null;
        public static MySqlConnection Conn
        {
            get
            {
                string username = global::HoSoLuuTru.Properties.Settings.Default.Username;
                string csdl = global::HoSoLuuTru.Properties.Settings.Default.Database;
                string host = global::HoSoLuuTru.Properties.Settings.Default.Server;
                string password = global::HoSoLuuTru.Properties.Settings.Default.Password;

                m_conn = new MySqlConnection();

                try
                {
                    m_conn.ConnectionString = "Password=" + password + ";User ID=" + username + ";Initial Catalog=" + csdl + ";Data Source=" + host;
                    m_conn.Open();
                }
                catch
                {
                    MessageBox.Show("Không thể kết nối đến server!\nVui lòng cấu hình lại",
                          "Thông báo", MessageBoxButtons.OK,
                          MessageBoxIcon.Error);
                    m_conn = null;
                }
                return m_conn;
            }
            set
            {
                m_conn = value;
            }
        }

        public static string ServerName
        {
            get { return global::HoSoLuuTru.Properties.Settings.Default.Server; }
            set
            {
                global::HoSoLuuTru.Properties.Settings.Default.Server = value;
                global::HoSoLuuTru.Properties.Settings.Default.Save();
            }
        }

        public static string Database
        {
            get { return global::HoSoLuuTru.Properties.Settings.Default.Database; }
            set
            {
                global::HoSoLuuTru.Properties.Settings.Default.Database = value;
                global::HoSoLuuTru.Properties.Settings.Default.Save();
            }
        }

        public static string Username
        {
            get { return global::HoSoLuuTru.Properties.Settings.Default.Username; }
            set
            {
                global::HoSoLuuTru.Properties.Settings.Default.Username = value;
                global::HoSoLuuTru.Properties.Settings.Default.Save();
            }
        }

        public static string Password
        {
            get { return global::HoSoLuuTru.Properties.Settings.Default.Password; }
            set
            {
                global::HoSoLuuTru.Properties.Settings.Default.Password = value;
                global::HoSoLuuTru.Properties.Settings.Default.Save();
            }
        }
        public static string SCon
        {
            get
            {
                return global::HoSoLuuTru.Properties.Settings.Default.sCon;
            }
            set
            {
                global::HoSoLuuTru.Properties.Settings.Default.sCon = value;
                global::HoSoLuuTru.Properties.Settings.Default.Save();
            }
        }
    }
}
